var _excluded = ["item", "defaultConfig"];
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
import { useMemo, useState } from 'react';
import { compHoc, history, registerComponent, iup, useExtendRef, useSubscribe } from "../../util";
import { convertHelpValues, fomatValues, getSubscribeFn, LayConfWrap } from "../common";
import { FormFieldSet as DefaultComp } from "./FormFieldSet";
import { jsx as _jsx } from "react/jsx-runtime";
export { default as FormFieldSet } from "./FormFieldSet";
export function FormSetComp(props) {
  var _ref2;
  var _ref = props,
    _ref$formConf = _ref.formConf,
    formConf = _ref$formConf === void 0 ? [] : _ref$formConf,
    opt = _ref.opt,
    dv = _ref.defaultValue,
    value = _ref.value,
    _ref$initSc = _ref.initSc,
    initSc = _ref$initSc === void 0 ? {} : _ref$initSc,
    outRef = _ref.outRef,
    busKey = _ref.busKey,
    onValuesChange = _ref.onValuesChange,
    formSetStyle = _ref.style,
    formSetClassName = _ref.className,
    rules = _ref.rules,
    collapsible = _ref.collapsible,
    defaultCollapse = _ref.collapse;
  var defaultValue = useMemo(function () {
    return value || dv || {};
  }, [value, dv]);
  var s = Array.isArray(formConf) ? formConf : formConf.children || formConf.fieldSets;
  var query = history.location.query;
  var optType = opt || query.opt;
  var defaultCollapsible = (_ref2 = collapsible !== null && collapsible !== void 0 ? collapsible : formConf.collapsible) !== null && _ref2 !== void 0 ? _ref2 : true;
  var collapse = defaultCollapse !== undefined ? defaultCollapse : formConf.collapse;
  function getFormatValues() {
    var undefinedValue = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '';
    var id = busKey || 'id';
    var opt = optType || defaultValue !== null && defaultValue !== void 0 && defaultValue[id] ? 'modifiedRow' : 'newRow';
    var g = fomatValues('COMMON_KEY', 'form', opt, id)(defaultValue);
    s.forEach(function (item, index) {
      g = g(getData(item.name || item.itemId || "fieldset".concat(index + 1), 'getValues', false, undefinedValue));
    });
    return g()['COMMON_KEY'];
  }
  function getValues() {
    var undefinedValue = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '';
    var labelInValue = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
    var v = {};
    s.forEach(function (item) {
      v = _objectSpread(_objectSpread({}, v), getData(item.name || item.itemId, 'getValues', false, undefinedValue, undefined, labelInValue));
    });
    return _objectSpread(_objectSpread({}, defaultValue), v);
  }
  function mergeHandler(v) {
    s.forEach(function (item, index) {
      var key = item.name || item.itemId || "fieldset".concat(index + 1);
      var t = iup.getCmp(key);
      t && t.getApi().mergeHandler(v[key]);
    });
  }
  useExtendRef(outRef, {
    mergeHandler: mergeHandler,
    getValues: getValues,
    getFormatValues: getFormatValues,
    validateForm: validateForm,
    isValid: isValid,
    validData: isValid,
    getItem: function getItem(name) {
      for (var i = 0; i < s.length; i++) {
        var _iup$getCmpApi, _iup$getCmpApi$getIte, _iup$getCmpApi2;
        var t = (_iup$getCmpApi = iup.getCmpApi(s[i].name || s[i].itemId || "fieldset".concat(i + 1))) === null || _iup$getCmpApi === void 0 || (_iup$getCmpApi$getIte = (_iup$getCmpApi2 = _iup$getCmpApi).getItem) === null || _iup$getCmpApi$getIte === void 0 ? void 0 : _iup$getCmpApi$getIte.call(_iup$getCmpApi2, name);
        if (t) return t;
      }
    },
    isChanged: function isChanged() {
      return s.some(function (item, index) {
        var _iup$getCmpApi3;
        return (_iup$getCmpApi3 = iup.getCmpApi(item.name || item.itemId || "fieldset".concat(index + 1))) === null || _iup$getCmpApi3 === void 0 ? void 0 : _iup$getCmpApi3.isChanged();
      });
    },
    getConfig: function getConfig() {
      return formConf;
    },
    getForm: function getForm(key) {
      var _iup$getCmp;
      return (_iup$getCmp = iup.getCmp(key)) === null || _iup$getCmp === void 0 ? void 0 : _iup$getCmp.getApi();
    },
    subscribe: getSubscribeFn(outRef)
  });
  function validateForm() {
    return Promise.all(s.map(function (item, index) {
      return getData(item.name || item.itemId || "fieldset".concat(index + 1), 'validateForm');
    }));
  }
  function isValid() {
    return Promise.all(s.map(function (item, index) {
      if (item.hidden) {
        return true;
      }
      return getData(item.name || item.itemId || "fieldset".concat(index + 1), 'isValid');
    })).then(function (result) {
      return result.every(function (r) {
        return r === true;
      });
    });
  }
  var defaultConfig = useMemo(function () {
    var cfg = _objectSpread({}, Array.isArray(formConf) ? {} : formConf);
    delete cfg.children;
    delete cfg.fieldSets;
    return cfg;
  }, [formConf]);
  var containerId = "".concat(props.id || props['data-cid'] || defaultConfig.name || 'formset');
  var _useState = useState({}),
    _useState2 = _slicedToArray(_useState, 2),
    c = _useState2[0],
    sc = _useState2[1];
  useSubscribe('__updateComputedDeps__', function (_ref3) {
    var args = _ref3.args;
    if (s !== null && s !== void 0 && s.some(function (i) {
      return iup.isFunction(i.computed);
    })) {
      sc(convertHelpValues(args[0] || {}));
    }
  }, ["formset.".concat(containerId)]);
  return /*#__PURE__*/_jsx("div", {
    className: iup.classNames('formSetContainer', formSetClassName),
    style: formSetStyle,
    children: s && s.map(function (itm, index) {
      var _ref4, _item$title, _item$collapsible;
      var item = iup.isFunction(itm.computed) ? _objectSpread(_objectSpread({}, itm), itm.computed(c)) : itm;
      var name = item.name || item.itemId || "fieldset".concat(index + 1);
      if (!item.children) {
        item.children = item.allfields;
      }
      return item.hidden ? null : /*#__PURE__*/_jsx(ItemRender, {
        defaultConfig: defaultConfig,
        item: item,
        title: (_ref4 = (_item$title = item.title) !== null && _item$title !== void 0 ? _item$title : item.label) !== null && _ref4 !== void 0 ? _ref4 : item.desTitle,
        initSc: initSc[name],
        id: name,
        cid: containerId,
        formSetRef: outRef,
        colspan: item.colspan || props.colspan,
        compact: item.compact || props.compact,
        layout: item.layout || props.layout || 'horizontal',
        opt: optType,
        rules: rules,
        panelProps: _objectSpread(_objectSpread({}, props.panelProps), item.panelProps),
        formStyle: _objectSpread(_objectSpread({}, props.formStyle), item.formStyle),
        size: item.size || props.size,
        disabled: props.disabled,
        collapsible: (_item$collapsible = item.collapsible) !== null && _item$collapsible !== void 0 ? _item$collapsible : defaultCollapsible && (item.title || item.title),
        collapse: typeof collapse === 'boolean' ? collapse : collapse === null || collapse === void 0 ? void 0 : collapse[name],
        value: defaultValue,
        onValuesChange: onValuesChange
      }, name);
    })
  });
}
function ItemRender(_ref5) {
  var item = _ref5.item,
    defaultConfig = _ref5.defaultConfig,
    others = _objectWithoutProperties(_ref5, _excluded);
  var cfg = useMemo(function () {
    return _objectSpread(_objectSpread({}, defaultConfig), item);
  }, [defaultConfig, item]);
  return /*#__PURE__*/_jsx(DefaultComp, _objectSpread({
    formConf: cfg
  }, others));
}
function getData(id, key) {
  var t = iup.getCmp(id).getApi();
  for (var _len = arguments.length, args = new Array(_len > 2 ? _len - 2 : 0), _key = 2; _key < _len; _key++) {
    args[_key - 2] = arguments[_key];
  }
  return t && t[key].apply(t, args) || {};
}
var FormSet = compHoc(LayConfWrap(FormSetComp), 'FormSet');
export default FormSet;
registerComponent({
  FormSet: FormSet
});